﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MiserBlogData.Entities;
using System.IO;
using System.Web;
using System.Reflection;
using NHibernate.Criterion;

namespace MiserBlogData.Helper
{
    public class RightBarHelper
    {
        protected string _templateHtml = @"<div class='Title'>{0}</div><ul>{1}</ul><br />";
        protected string _contentHtml = "<li>{0}({1})</li>";

        public string TemplateHtml { get { return _templateHtml; } }
        public string ContentHtml { get { return _contentHtml; } }

        public UserVIP Person { get; set; }


        #region

        //protected static UserVIP GetHtmlTemplateInfo(int uid)
        //{
        //    //从数据库插叙该用户模板的位置
        //    UserVIP vip = UserVIP.GetUser<UserVIP>(uid);
        //    return vip;
        //}

        //protected string GetHtmlTemplate(IList<UserLeftBar> listUserBar)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    string path = HttpContext.Current.Server.MapPath(@"\LeftBar\");
        //    foreach (var item in listUserBar)
        //    {

        //    }
        //    return sb.ToString();
        //}

        public static string ParseHtmlTemplate(int uid)
        {
            
            StringBuilder sb = new StringBuilder();
            

            using (MiserDB.DataHelp.QueryHelper data = new MiserDB.DataHelp.QueryHelper())
            {
                

                //Dictionary<string, List<SimpleExpression>> where = new Dictionary<string, List<SimpleExpression>>();
                //List<SimpleExpression> whereItem = new List<SimpleExpression>();
                //whereItem.Add(Restrictions.Eq("Id", uid));
                //where.Add("User", whereItem);


                List<MiserDB.DataHelp.QueryHelper.WhereKey> whereKey = new List<MiserDB.DataHelp.QueryHelper.WhereKey>();
                whereKey.Add(new MiserDB.DataHelp.QueryHelper.WhereKey("User", "user"));

                List<SimpleExpression> whereItem = new List<SimpleExpression>();
                whereItem.Add(Restrictions.Eq("user.Id", uid));
                //whereItem.Add(Restrictions.Eq("State", (short)DataState.Release));

                RightBarHelper bar = new RightBarHelper();
                UserVIP vip = new UserVIP();
                vip.CategoriesList = data.GetList<Categories>(whereKey, whereItem, null);
                bar.Person = vip;
                IList<UserLeftBar> list = data.GetList<UserLeftBar>(whereKey, whereItem, null);

                foreach (var item in list)
                {
                    string className = item.LeftBar.ClassName;
                    string assemblyName = className.Substring(0, className.LastIndexOf('.'));
                    object obj = Assembly.Load("MiserBlogData").CreateInstance(className);
                    if (obj is IParseRightBar)
                    {
                        IParseRightBar p = (IParseRightBar)obj;
                        string htmlContent = p.ParseRightBar(bar);
                        if (!string.IsNullOrEmpty(htmlContent))
                        {
                            sb.Append(string.Format(bar.TemplateHtml, item.LeftBar.Name, htmlContent));
                        }
                    }

                }

            }
            return sb.ToString();
        }

        #endregion


    }
}
